package userInteraction;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@SuppressWarnings("serial")
public class loginService implements Serializable {
	public Boolean checkLogin(String user, String pas) throws Exception{
		Connection con = null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/licenseDb","root", "root");

			PreparedStatement query = (PreparedStatement) con.prepareStatement("select * from Logins where emailAddress=? and password=?");
			query.setString(1, user);
			query.setString(2, pas);
			ResultSet rs = query.executeQuery();

			rs.last();			
			Integer rowCount = rs.getRow();
			if (rowCount==1){
				//System.out.println("Autentificare valida!");
				return true;
			}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				if(con != null)
					con.close();
			} 
			catch(Exception e) {}
		}
		//System.out.println("Autentificare esuata!");
		return false;
	}

	public String whoIsLogged(String user)throws Exception{
		String whoIs = null;
		Connection con = null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/licenseDb","root", "root");

			PreparedStatement query = con.prepareStatement("select Customer.firstName, Customer.lastName from Customer where emailAddress=?");
			query.setString(1, user);
			ResultSet rs = query.executeQuery();

			while(rs.next()){
				whoIs = rs.getString("Customer.firstName") + " " + rs.getString("Customer.lastName");
			}

		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try{
				if(con != null)
					con.close();
			}
			catch(Exception e){}
		}
		return whoIs;
	}
}
